﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    public partial class ITXSETUP 
    { 

        BindingListCollectionView cvdlgTran
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgTran.ItemsSource);
            }
        }
      
          #region=======MULLANG=========
        private void butRepaire_Click(object sender, RoutedEventArgs e)
        {
            m_Data.ProcessFiles("");
        }

        private void butToDo_Click(object sender, RoutedEventArgs e)
        {
            string filter = "MLKEY=EN";
            cvdlgTran.CustomFilter = filter;
        }
        private void butNEWMULLANG_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwMULLANG == null)
                    return;
                DataRow dwNew = SetDetail.ICC_MULLANG.NewRow();
                dwNew.ItemArray =dwMULLANG.ItemArray;
                dwNew["ROWNID"] = "0";
                SetDetail.ICC_MULLANG.Rows.Add(dwNew);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butMULLANGITXLoad_Click(object sender, RoutedEventArgs e)
        {
            cvdlgTran.CustomFilter = null;
            DataSet setResult = null;
            try
            {
                setResult = m_Data.LoadITXMULLANG(Mandant);
                SetMaster.Clear();
                SetMaster.Merge(setResult, false, MissingSchemaAction.Ignore);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetMaster.Clear();
                SetMaster.Merge(setResult, false, MissingSchemaAction.Ignore);
                SetMaster.AcceptChanges();
            }
        }
        private void butMULLANGCopy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwMULLANG == null)
                    return;
                DataRow dwNew = SetDetail.ICC_MULLANG.NewRow();
                dwNew.ItemArray = dwMULLANG.ItemArray;
                dwNew["ROWNID"] = "0";
                dwNew["MLLOC"] = txtMLloc.Text.Trim();
                SetDetail.ICC_MULLANG.Rows.Add(dwNew);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butMULLANGEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_MULLANG);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            finally
            {

            }
        }
        private void butMULLANGCopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_MULLANG);
                foreach (DataRow RX in SetDetail.ICC_MULLANG.Rows)
                {
                    RX["ROWNID"] = "0";
                    RX["MLLOC"] = txtMLloc.Text.Trim();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }

        }
        private void butMULLANGSave_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                m_Data.SaveMULLANG(SetDetail, "00");
                /*if (vwMULLANG.View != null)
                    vwMULLANG.View.MoveCurrentToNext();*/
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter
                || e.Key == System.Windows.Input.Key.Return)
            {
                butSame_Click(null, null);
            } 
        }

        private void butDiscovery_Click(object sender, RoutedEventArgs e)
        {
            bgWoker.RunWorkerAsync();
        }

        private void butSameKeyEdit_Click(object sender, RoutedEventArgs e)
        {
            SetDetail.Clear();
            DataSet setResult = null;
            try
            {
                if (dwMULLANG == null)
                    return;
                string filter = "MLKEY='" + dwMULLANG["MLKEY"].ToString() + "'";
                DataRow[] rows = SetMaster.ICC_MULLANG.Select(filter);
                SetDetail.ICC_MULLANG.Merge(rows.CopyToDataTable());
                if (vwTMPMULLANG.View != null)
                    vwTMPMULLANG.View.MoveCurrentToNext();
                //txtEN.SelectAll();*
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void butSame_Click(object sender, RoutedEventArgs e)
        {
            if (dwTMPMULLANG == null)
                return;
            foreach (DataRow row in SetDetail.ICC_MULLANG.Rows)
            {
                row["EN"] = dwTMPMULLANG["EN"];              
            }
           var query = from DataRow rx in SetMaster.ICC_MULLANG.AsEnumerable()
                       where rx["MLKEY"].ToString() == dwTMPMULLANG["MLKEY"].ToString()
                          select rx;
              foreach (DataRow rx in query)
              {
                  rx["EN"] = dwTMPMULLANG["EN"];
              }
            butMULLANGSave_Click(null, null);
            butSameKeyEdit_Click(null, null);
        }
        #endregion 
    }
}
